<template>
	<view class="pageBox">
		<!-- 标题栏 -->
		<head-navbar :back="true">
			<view>申请商家</view>
		</head-navbar>

		<view class="content">
			<u-form :model="form" ref="uForm" :border-bottom="true" :label-width="labelWidth" label-position="top">
				<u-form-item label="企业名称">
					<u-input v-model="form.companyName" placeholder="请输入企业名称" />
				</u-form-item>
				<u-form-item label="统一社会信用代码">
					<u-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
				</u-form-item>
				<u-form-item label="法人身份证号">
					<u-input v-model="form.identityCard" />
				</u-form-item>
				<u-form-item label="经营范围">
					<u-input v-model="form.businessScope" type="textarea" />
				</u-form-item>
			</u-form>
			<view class="list">
				<view class="item">
					<view class="title">
						<text class="left">*</text>
						<text class="right">1.请上传营业执照</text>
					</view>
					<view class="upLoadBox" @click="uploadImg('businessLicenseImg')" v-if="param.businessLicenseImg == null  || param.businessLicenseImg == ''">点击上传图片</view>
					<image :src="param.businessLicenseImg" mode="aspectFill" v-else class="upLoadBox"></image>
				</view>
				<view class="item">
					<view class="title">
						<text class="left">*</text>
						<text class="right">2.请上传法人身份证正反照片</text>
					</view>
					<view class="bigBox">
						<view class="upLoadBox" @click="uploadImg('OnidCodePhoto')" v-if="param.OnidCodePhoto == null  || param.OnidCodePhoto == ''">点击上传图片</view>
						<image :src="param.OnidCodePhoto" mode="aspectFill" v-else class="upLoadBox"></image>

						<view class="upLoadBox" style="display: inline-block;" @click="uploadImg('OffidCodePhoto')" v-if="param.OffidCodePhoto == null  || param.OffidCodePhoto == ''">点击上传图片</view>
						<image :src="param.OffidCodePhoto" v-else class="upLoadBox"></image>
					</view>

				</view>
				<view class="item">
					<view class="title">
						<text class="right" style="padding: 0;">3.请上传经营许可证照片（非必须）</text>
					</view>
					<view class="upLoadBox" @click="uploadImg('permitImg')" v-if="param.permitImg == null  || param.permitImg == ''">点击上传图片</view>
					<image :src="param.permitImg" mode="aspectFill" v-else class="upLoadBox"></image>
				</view>
			</view>
		</view>
		<view style="margin-top: 30upx;" class="sumitBtn" @click="sumitBtn">提交申请</view>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from '../../../../utils/imgTool.js';
	import * as api from '@/api/user/pwd.js';
	import config from '@/config/index.js';
	export default {
		data() {
			return {
				action: '',
				labelWidth: 250,
				form: {
					creditCode: '', //统一社会信用代码
					identityCard: '', //身份证号
					businessScope: '', //经营范围
					identityCardImgs: '', //法人身份证
					businessLicenseImg: '', //营业执可照
					permitImg: '' //经营许可证
				},
				param: {
					businessLicenseImg: '',
					OnidCodePhoto: '',
					OffidCodePhoto: '',
					permitImg: '',
				},
				request: {
					creditCode: '', //统一社会信用代码
					identityCard: '', //身份证号
					businessScope: '', //经营范围
					identityCardImgs: '', //法人身份证
					businessLicenseImg: '', //营业执可照
					permitImg: '' //经营许可证
				}
			}
		},
		created() {
			this.getMerchantApplyStatus()
		},
		methods: {
			async getMerchantApplyStatus() {
				try {
					const ret = await api.merchantApplyStatus();
					console.log(ret)
					if(ret.data.obj == 0) {
						this.$refs.uTips.show({
							title: '审核中',
							type: 'primary',
							duration: '2300'
						})
					}else if(ret.data.obj == 1){
						this.$refs.uTips.show({
							title: '审核通过',
							type: 'success ',
							duration: '2300'
						})
					}else{
						this.$refs.uTips.show({
							title: '审核不通过',
							type: 'error ',
							duration: '2300'
						})
					}
				} catch (error) {

				}
			},
			uploadImg(type) {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: (res) => {
						console.log(res)
						if (type == 'OnidCodePhoto' || type == 'OffidCodePhoto') {
							this.request[type] = res.tempFilePaths[0];
						} else {
							this.request[type] = res.tempFilePaths[0];
						}
						uni.getImageInfo({
							src: res.tempFiles[0].path,
							success: (res) => {
								pathToBase64(res.path)
									.then(base64 => {
										this.param[type] = base64;
									})
									.catch(error => {
										this.utils.toast('转换Base64失败');
									})
							},
							fail: (err) => {
								this.utils.toast('上传图片失败');
							}
						})
					},
					fail: (err) => {

					}
				})
			},
			sumitBtn() {
				this.runSumbtn();
			},
			async runSumbtn() {
				let list = [{
						name: 'permitImg',
						uri: this.request.permitImg
					},
					{
						name: 'businessLicenseImg',
						uri: this.request.businessLicenseImg
					},
					{
						name: 'identityCardImgs',
						uri: this.param.OnidCodePhoto
					},
					{
						name: 'identityCardImgs',
						uri: this.param.OffidCodePhoto
					}
				]
				let token = '';
				var that = this;
				try {
					token = uni.getStorageSync('token');
					if (token) {
						console.log(token);
						uni.uploadFile({
							url: config.apiServer + '/front/merchant/apply',
							files: list,
							header: {
								'token': token
							},
							formData: {
								'creditCode': that.form.creditCode,
								'identityCard': that.form.identityCard,
								'businessScope': that.form.businessScope,
								'companyName': that.form.companyName
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes.data);
								if (uploadFileRes.data.code == 0) {
									that.utils.total('提交成功')
								} else {
									that.utils.total(uploadFileRes.data.msg)
								}

							},
							fail(err) {
								that.utils.toast(err.errMsg);
							}
						});
					}
				} catch (e) {
					console.log(e)
				}
			}
		}
	}
</script>

<style lang="scss">
	@import './index.scss';
</style>
